package com.jinxin.phonecloudmusic.myapplication.fragment.fra;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.jinxin.phonecloudmusic.myapplication.R;
import com.jinxin.phonecloudmusic.myapplication.adapter.MusicListAdapter;
import com.jinxin.phonecloudmusic.myapplication.base.BaseFragment;
import com.jinxin.phonecloudmusic.myapplication.entity.GroupBean.BillboardList;
import com.jinxin.phonecloudmusic.myapplication.entity.MusicBean.SongList;
import com.jinxin.phonecloudmusic.myapplication.event.MusicControlEvent;
import com.jinxin.phonecloudmusic.myapplication.presenter.IPresenter.IMainPagePresenter;
import com.jinxin.phonecloudmusic.myapplication.utils.RxBus;
import com.squareup.picasso.Picasso;


import java.util.List;

import butterknife.BindView;

/**
 * Created by ZJ on 2017/10/17 0017.
 * 主页碎片
 */

public class MusicMainFragment extends BaseFragment /*implements IMainPageView*/ {
    @BindView(R.id.rl_music_list_hot)
    RelativeLayout rlMusicListHot;
    @BindView(R.id.lv_hot_songs)
    ListView lvHotSongs;
    @BindView(R.id.iv_music_list_hot)
    ImageView ivMusicListHot;
    //新歌榜
    @BindView(R.id.rl_music_list_new)
    RelativeLayout rlMusicListNew;
    @BindView(R.id.lv_new_songs)
    ListView lvNewSongs;
    @BindView(R.id.iv_music_list_new)
    ImageView ivMusicListNew;
    //经典歌曲榜
    @BindView(R.id.rl_music_list_classical)
    RelativeLayout rlMusicListClassical;
    @BindView(R.id.lv_classical_songs)
    ListView lvClassicalSongs;
    @BindView(R.id.iv_music_list_classical)
    ImageView ivMusicClassical;
    //欧美金曲榜
    @BindView(R.id.rl_music_list_western)
    RelativeLayout rlMusicListWestern;
    @BindView(R.id.lv_western_songs)
    ListView lvWesternSongs;
    @BindView(R.id.iv_music_list_western)
    ImageView ivMusicListWestern;
    //影视金曲
    @BindView(R.id.rl_music_list_teleplay)
    RelativeLayout rlMusicListTeleplay;
    @BindView(R.id.lv_teleplay_songs)
    ListView lvTeleplaySongs;
    @BindView(R.id.iv_music_list_teleplay)
    ImageView ivMusicListTeleplay;
    //网络歌曲
    @BindView(R.id.rl_music_list_network)
    RelativeLayout rlMusicListNetwork;
    @BindView(R.id.lv_network_songs)
    ListView lvNetworkSongs;
    @BindView(R.id.iv_music_list_network)
    ImageView ivMusicListNetwork;
    @BindView(R.id.rl_main_local)
    RelativeLayout rlMainLocal;
    @BindView(R.id.rl_main_collect)
    RelativeLayout rlMainCollect;
    @BindView(R.id.rl_main_recent_play)
    RelativeLayout rlMainRecentPlay;
    @BindView(R.id.iv_main_search)
    ImageView ivMainSearch;

    private IMainPagePresenter iMainPagePresenter;
    int[] type = new int[]{1, 2, 21, 22, 24, 25};

    private MusicListAdapter hotMusicListAdapter;
    private MusicListAdapter newMusicListAdapter;
    private MusicListAdapter classicalMusicListAdapter;
    private MusicListAdapter westernMusicListAdapter;
    private MusicListAdapter teleplayMusicListAdapter;
    private MusicListAdapter networkMusicListAdapter;


    //歌曲中转站
    private List<SongList> hotSongs;
    private List<SongList> allHotSongs;
    private List<SongList> newSongs;
    private List<SongList> allNewSongs;
    private List<SongList> classicalSongs;
    private List<SongList> allClassicalSongs;
    private List<SongList> westernSongs;
    private List<SongList> allWesternSongs;
    private List<SongList> teleplaySongs;
    private List<SongList> allTeleplaySongs;
    private List<SongList> networkSongs;
    private List<SongList> allNetworkSongs;


    private List<BillboardList> list;
    private BillboardList s;

   /* @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        iMainPagePresenter=new IMainPagePresentImp(this);
        //遍历数组，请求各类榜单
        for (int t: type) {
            iMainPagePresenter.allBillboardList("billboard", t, 20, 0);
        }
    }*/

    @Override
    public View inflate(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fra_main, container, false);
    }

    @Override
    public void initData() {
        if (null!=getArguments()){
            list= (List<BillboardList>) getArguments().getSerializable("mainData");
            jumbMusicLIstDetail(list);

        }
        ivMainSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RxBus.getDefault().post(new MusicControlEvent(1000,"",""));
            }
        });
        rlMainLocal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {//本地音乐
                RxBus.getDefault().post(new MusicControlEvent(33,"本地音乐"));
            }
        });
        rlMainCollect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RxBus.getDefault().post(new MusicControlEvent(34,"收藏音乐"));
            }
        });
        rlMainRecentPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RxBus.getDefault().post(new MusicControlEvent(35,"最近播放"));
            }
        });
    }

    public void jumbMusicLIstDetail(List<BillboardList> list) {
        //遍历确定各个类型
        for (BillboardList s : list) {
            if (s.getBillboard().getBillboard_type().equals("2")) {//热歌
                if (null == hotMusicListAdapter) {
                    hotMusicListAdapter = new MusicListAdapter(getActivity(), R.layout.item_main_music_list, s.getLists());
                } else {
                    hotMusicListAdapter.replaceAll(s.getLists());
                }
                lvHotSongs.setAdapter(hotMusicListAdapter);
                Picasso.with(getActivity()).load(s.getBillboard().getPic_s210()).placeholder(R.drawable.background).error(R.drawable.background).into(ivMusicListHot);
                //点击事件
                rlMusicListHot.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //跳转到列表的二级界面
                        RxBus.getDefault().post(new MusicControlEvent(31, 2, ""));
                    }
                });
            }
            if (s.getBillboard().getBillboard_type().equals("1")) {//新歌榜
                if (null == newMusicListAdapter) {
                    newMusicListAdapter = new MusicListAdapter(getActivity(), R.layout.item_main_music_list, s.getLists());
                } else {
                    newMusicListAdapter.replaceAll(s.getLists());
                }
                lvNewSongs.setAdapter(newMusicListAdapter);
                Picasso.with(getActivity()).load(s.getBillboard().getPic_s210()).placeholder(R.drawable.background).error(R.drawable.background).into(ivMusicListNew);
                rlMusicListNew.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        RxBus.getDefault().post(new MusicControlEvent(31, 1, ""));
                    }
                });
            }
            if (s.getBillboard().getBillboard_type().equals("22")) {//经典歌曲榜
                if (null == classicalMusicListAdapter) {
                    classicalMusicListAdapter = new MusicListAdapter(getActivity(), R.layout.item_main_music_list, s.getLists());
                } else {
                    classicalMusicListAdapter.replaceAll(s.getLists());
                }
                lvClassicalSongs.setAdapter(classicalMusicListAdapter);
                Picasso.with(getActivity()).load(s.getBillboard().getPic_s210()).placeholder(R.drawable.background).error(R.drawable.background).into(ivMusicClassical);
                rlMusicListClassical.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        RxBus.getDefault().post(new MusicControlEvent(31, 22, ""));
                    }
                });
            }
            if (s.getBillboard().getBillboard_type().equals("21")) {//欧美金曲榜
                if (null == westernMusicListAdapter) {
                    westernMusicListAdapter = new MusicListAdapter(getActivity(), R.layout.item_main_music_list, s.getLists());
                } else {
                    westernMusicListAdapter.replaceAll(s.getLists());
                }
                lvWesternSongs.setAdapter(westernMusicListAdapter);
                Picasso.with(getActivity()).load(s.getBillboard().getPic_s210()).placeholder(R.drawable.background).error(R.drawable.background).into(ivMusicListWestern);
                rlMusicListWestern.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        RxBus.getDefault().post(new MusicControlEvent(31, 21, ""));
                    }
                });
            }
            if (s.getBillboard().getBillboard_type().equals("24")) {//影视金曲
                if (null == teleplayMusicListAdapter) {
                    teleplayMusicListAdapter = new MusicListAdapter(getActivity(), R.layout.item_main_music_list, s.getLists());
                } else {
                    teleplayMusicListAdapter.replaceAll(s.getLists());
                }
                lvTeleplaySongs.setAdapter(teleplayMusicListAdapter);
                Picasso.with(getActivity()).load(s.getBillboard().getPic_s210()).placeholder(R.drawable.background).error(R.drawable.background).into(ivMusicListTeleplay);
                rlMusicListTeleplay.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        RxBus.getDefault().post(new MusicControlEvent(31, 24, ""));
                    }
                });
            }
            if (s.getBillboard().getBillboard_type().equals("25")) {//网络歌曲
                if (null == networkMusicListAdapter) {
                    networkMusicListAdapter = new MusicListAdapter(getActivity(), R.layout.item_main_music_list, s.getLists());
                } else {
                    networkMusicListAdapter.replaceAll(s.getLists());
                }
                lvNetworkSongs.setAdapter(networkMusicListAdapter);
                Picasso.with(getActivity()).load(s.getBillboard().getPic_s210()).placeholder(R.drawable.background).error(R.drawable.background).into(ivMusicListNetwork);
                rlMusicListNetwork.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        RxBus.getDefault().post(new MusicControlEvent(31, 25, ""));
                    }
                });
            }
        }
    }

    //跳转到副界面
    public void jumb() {

    }
}
